package com.bulletelc.apollopre.module.main.center.intention.list;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.Toast;

import com.bulletelc.apollopre.R;
import com.bulletelc.apollopre.adapter.ViewPageAdapter;
import com.bulletelc.apollopre.entity.IntentionBean;
import com.bulletelc.apollopre.framework.MVPBaseActivity;
import com.bulletelc.apollopre.module.main.center.intention.list.fragment.IntentionListFragment;
import com.bulletelc.apollopre.widget.FoolBar;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/5/16.
 * 我的意向单
 */

public class IntentionListActivity extends MVPBaseActivity<IntentionListContract.View, IntentionListPresenter> implements IntentionListContract.View,
        FoolBar.ClickCallback {

    // views
    private FoolBar flBar;
    private ViewPager vpMain;
    private TabLayout tlMain;

    // data
    private List<IntentionBean> intentionBeanList; // 所有意向
    private IntentionListFragment quotatedFragment;
    private IntentionListFragment completeFragment;
    private ViewPageAdapter viewPageAdapter;
    private List<Fragment> fragmentList;

    @Override
    protected void onStart() {
        super.onStart();
        presenter.getIntentionList();
    }

    @Override
    public int getLayoutRes() {
        return R.layout.intention_list_activity;
    }

    @Override
    public void assignViews() {
        flBar = findView(R.id.flBar);
        vpMain = findView(R.id.vpMain);
        tlMain = findView(R.id.tlMain);
    }

    @Override
    public void setListener() {
        flBar.setClickCallback(this);
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        flBar.setTitle("我的定制");
        fragmentList = new ArrayList<>();
        List<String> title = new ArrayList<>();
        intentionBeanList = new ArrayList<>();

        quotatedFragment = new IntentionListFragment(); // 报价
        completeFragment = new IntentionListFragment(); // 完成

        fragmentList.add(quotatedFragment);
        fragmentList.add(completeFragment);
        title.add("已报价");
        title.add("已完成");

        viewPageAdapter = new ViewPageAdapter(getSupportFragmentManager(), fragmentList, title);
        vpMain.setAdapter(viewPageAdapter);
        tlMain.setupWithViewPager(vpMain);

        presenter.getIntentionList();
    }

    @Override
    public void onLeftClick() {
        finish();
    }

    @Override
    public void onRightClick() {

    }

    @Override
    protected IntentionListPresenter createPresenter() {
        return new IntentionListPresenter();
    }

    @Override
    public void onSuccessIntentionList(List<IntentionBean> intentionBeanList) {
        if (intentionBeanList.size() > 0) {
            this.intentionBeanList.clear();
            this.intentionBeanList.addAll(intentionBeanList);
            quotatedFragment.sortQuotationState(intentionBeanList);
            completeFragment.sortCompleteState(intentionBeanList);
        }
    }

    @Override
    public void onErrorIntentionList(String error) {
        Toast.makeText(this, R.string.network_msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFailIntentionList(String fail) {
        Toast.makeText(this, fail, Toast.LENGTH_SHORT).show();
    }

}
